<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The MongoCommandCursor class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongocursorinterface.timeout.html">« MongoCursorInterface::timeout</a></li>
      <li style="float: right;"><a href="mongocommandcursor.batchsize.html">MongoCommandCursor::batchSize »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mongo.core.html">Core Classes</a></li>
    <li>The MongoCommandCursor class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.mongocommandcursor" class="reference">

 <h1 class="title">The MongoCommandCursor class</h1>
 

 <div class="partintro"><p class="verinfo">(PECL mongo &gt;=1.5.0)</p>

  
  <div class="section" id="mongocommandcursor.intro">
   <h2 class="title">Introduction</h2>
   <p class="para">
    A command cursor is similar to a <a href="class.mongocursor.html" class="classname">MongoCursor</a> except
    that you use it for iterating through the results of a database command
    instead of a normal query. Command cursors are useful for iterating over
    large result sets that might exceed the document size limit (currently 16MB)
    of a single <span class="function"><a href="mongodb.command.html" class="function">MongoDB::command()</a></span> response.
   </p>
   <p class="para">
    While you can create command cursors using
    <span class="function"><a href="mongocommandcursor.construct.html" class="function">MongoCommandCursor::__construct()</a></span> or the
    <span class="function"><a href="mongocommandcursor.createfromdocument.html" class="function">MongoCommandCursor::createFromDocument()</a></span> factory method,
    you will generally want to use command-specific helpers such as
    <span class="function"><a href="mongocollection.aggregatecursor.html" class="function">MongoCollection::aggregateCursor()</a></span>.
   </p>
   <p class="para">
    Note that the cursor does not &quot;contain&quot; the database command&#039;s results; it
    just manages iteration through them. Thus, if you print a cursor (f.e. with
    <span class="function"><strong>var_dump()</strong></span> or <span class="function"><strong>print_r()</strong></span>), you will see
    the cursor object but not the result documents.
   </p>
  </div>

  <div class="section">
   <h2 class="title">Cursor Stages</h2>
   <p class="para">
    A <strong class="classname">MongoCommandCursor</strong> has two &quot;life stages&quot;: pre-
    and post- command. When a cursor is created, it has not yet contacted the
    database, so it is in its pre-command state. When the client first attempts
    to get a result (by calling <span class="function"><a href="mongocommandcursor.rewind.html" class="function">MongoCommandCursor::rewind()</a></span>,
    directly or indirectly), the cursor moves into the post-command state.
   </p>
   <p class="para">
    The command cursor&#039;s batch size and socket timeout may be configured in both
    the pre- and post- command states.
   </p>
   <p class="para">
    <div class="example" id="mongocommandcursor.stages.adding-options">
     <p><strong>Example #1 Adding options to <strong class="classname">MongoCommandCursor</strong></strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$cursor&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoCommandCursor</span><span style="color: #007700">(...);<br /><br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">batchSize</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
  

  <div class="section" id="mongocommandcursor.synopsis">
   <h2 class="title">Class synopsis</h2>

   
   <div class="classsynopsis">
    <div class="ooclass"></div>

    
    <div class="classsynopsisinfo">
     <span class="ooclass">
      <strong class="classname">MongoCommandCursor</strong>
     </span>
     <span class="oointerface">implements 
      <span class="interfacename"><a href="class.mongocursorinterface.html" class="interfacename">MongoCursorInterface</a></span>
     </span>
     <span class="oointerface">, 
      <span class="interfacename"><strong class="interfacename">Iterator</strong></span>
     </span>
     {</div>
    
    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.batchsize.html" class="methodname">batchSize</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$batchSize</code></span>): <span class="type"><a href="class.mongocommandcursor.html" class="type MongoCommandCursor">MongoCommandCursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.construct.html" class="methodname">__construct</a></span>(<span class="methodparam"><span class="type"><a href="class.mongoclient.html" class="type MongoClient">MongoClient</a></span> <code class="parameter">$connection</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$ns</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$command</code><span class="initializer"> = array()</span></span>)</div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="mongocommandcursor.createfromdocument.html" class="methodname">createFromDocument</a></span>(<span class="methodparam"><span class="type"><a href="class.mongoclient.html" class="type MongoClient">MongoClient</a></span> <code class="parameter">$connection</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$hash</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$document</code></span>): <span class="type"><a href="class.mongocommandcursor.html" class="type MongoCommandCursor">MongoCommandCursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.current.html" class="methodname">current</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.dead.html" class="methodname">dead</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.getreadpreference.html" class="methodname">getReadPreference</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.info.html" class="methodname">info</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.key.html" class="methodname">key</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.next.html" class="methodname">next</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.rewind.html" class="methodname">rewind</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.setreadpreference.html" class="methodname">setReadPreference</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$read_preference</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$tags</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.mongocommandcursor.html" class="type MongoCommandCursor">MongoCommandCursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.timeout.html" class="methodname">timeout</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$ms</code></span>): <span class="type"><a href="class.mongocommandcursor.html" class="type MongoCommandCursor">MongoCommandCursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="mongocommandcursor.valid.html" class="methodname">valid</a></span>(): <span class="type">bool</span></div>

   }</div>
   

  </div>

  <div class="section">
   <h2 class="title">See Also</h2>
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="mongodb.command.html" class="methodname">MongoDB::command()</a></span></li>
    <li class="member"><span class="methodname"><a href="mongocollection.aggregatecursor.html" class="methodname">MongoCollection::aggregateCursor()</a></span></li>
   </ul>
  </div>

 </div>

 



 





 





 





 





 












 





 





 





 












 





 



<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="mongocommandcursor.batchsize.html">MongoCommandCursor::batchSize</a> — Limits the number of elements returned in one batch</li><li><a href="mongocommandcursor.construct.html">MongoCommandCursor::__construct</a> — Create a new command cursor</li><li><a href="mongocommandcursor.createfromdocument.html">MongoCommandCursor::createFromDocument</a> — Create a new command cursor from an existing command response document</li><li><a href="mongocommandcursor.current.html">MongoCommandCursor::current</a> — Returns the current element</li><li><a href="mongocommandcursor.dead.html">MongoCommandCursor::dead</a> — Checks if there are results that have not yet been sent from the database</li><li><a href="mongocommandcursor.getreadpreference.html">MongoCommandCursor::getReadPreference</a> — Get the read preference for this command</li><li><a href="mongocommandcursor.info.html">MongoCommandCursor::info</a> — Gets information about the cursor's creation and iteration</li><li><a href="mongocommandcursor.key.html">MongoCommandCursor::key</a> — Returns the current result's index within the result set</li><li><a href="mongocommandcursor.next.html">MongoCommandCursor::next</a> — Advances the cursor to the next result</li><li><a href="mongocommandcursor.rewind.html">MongoCommandCursor::rewind</a> — Executes the command and resets the cursor to the start of the result set</li><li><a href="mongocommandcursor.setreadpreference.html">MongoCommandCursor::setReadPreference</a> — Set the read preference for this command</li><li><a href="mongocommandcursor.timeout.html">MongoCommandCursor::timeout</a> — Sets a client-side timeout for this command</li><li><a href="mongocommandcursor.valid.html">MongoCommandCursor::valid</a> — Checks if the cursor is reading a valid result</li></ul>
</div>
</div></div></body></html>